#!/bin/bash

set -e
set -x

iplist_path=config
i=1
while read -r line
do
    case $line in
        *'#'*)
            continue
        ;;
        *'end'*)
            break
        ;;
        *)
            IFS=',' read -ra parts <<< "$line"
        ;;
    esac

    pass="${parts[0]}"
    address="${parts[1]}"
    hostname="${parts[2]}"
    server_ip="${parts[3]}"

    if [ $address ];then
        \cp user.sql.tpl user.sql
        sed -i "s/server_ip/$server_ip/" user.sql
        sshpass -p$pass scp user.sql $address:/usr/local/mysql/
        sshpass -p$pass ssh -n $address 'mysql -uroot -pAbc_123456 < /usr/local/mysql/user.sql'
    fi
done < $iplist_path

set +x
set +e